<?php
/***************************
 * header frontend for website.
 ***************************/
session_start();
require_once('../config.php'); // config.php在根目录下
require_once( '../includes/class.user.php');
require_once( '../includes/class.form.php');
$login_success = false;

if(isset($_POST['login_submit'])){
	$username = postvar('username');
	$password = encrypt(postvar('password'));
	
	if(isset($_POST['remember'])){
		$remember = true;
	}else{
		$remember = false;
	}
	$user = new User();
	$user->username = $username;
	$user->password = $password;
	
	if(!$user->login($remember)){
		if(getSession('login_error')==null){
			die('login error was not catched')	;
		}
		
		//login failed, get error from session
		printSession('login_error');
	}else{
		$login_success = true;
	}
}

if(isset($_POST['register_submit'])){
	$username = postvar('username');
	$email = postvar('email');
	$password = postvar('password');
	$password_confirm = postvar('password_confirm');
	
	$user = new User();
	$reg_result = $user->register($username, $password, $password_confirm, $email);
	if(!$reg_result){
		printSession('reg_error');
	}else{
		header('Location: '.HOME_URL.'user/signup_success.php?uid='.$reg_result);
	}
}
	
require_once('../includes/header.php');
if($login_success){
	js_redirect("登录成功，正在转至主页", USER_HOME_URL);
}
?>

<div class='form_column'>
	<div class="message">
		<?php echo (isset($login_error))?$login_error:'';
		?>
	</div>
	<div class='login'>
		<?php
		Form::openForm('','post');
		Form::createInputField('text', '用户名', 'username','',true);
		Form::createInputField('password', '密码', 'password','',true);
		Form::createCheckbox('remember', '自动登录');
		Form::submitButton('login_submit', '登录','class="btn btn-primary"');
		Form::closeForm();
		?>
	</div>
		<?php
		Form::createJSButton('还没有账号？30秒注册一个！',"$('.signup').show();",'','class="btn btn-link pull-right"');
		Form::clearFix();
		?>
	<?php
	if (isset($_POST['register_submit'])) {
		echo '<div class="signup" style="display: block;">';
	}else{
		echo '<div class="signup" style="display: none;">';
	}
	?>
	<!--<script type="text/javascript" src="../scripts/user_check.js"></script>-->

		<?php
			Form::openForm('','post','register_form','register_form');
			Form::createInputField('text', '用户名', 'username','',true,'');
			Form::createInputField('password', '密码', 'password','',true,'');
			Form::createInputField('password', '确认密码', 'password_confirm','',true,'');
			Form::createInputField('text', '邮箱', 'email','',true,'');
		?>
			<!-- 验证码 -->
			<img id="captcha" src="../libs/securimage/securimage_show.php" alt="验证码" width="150px" style="border:#999 1px solid" />
			<a href="#" onclick="document.getElementById('captcha').src = '../libs/securimage/securimage_show.php?' + Math.random(); return false"><img src="../libs/securimage/images/refresh.png" width="25px" /></a>
			<br />
			<label for="code">验证码</label>
			<input id="code" type="text" name="captcha_code" size="10" maxlength="6" />
			<!-- /验证码 -->
		<?php
			Form::submitButton('register_submit','加入我们','class="btn btn-primary"');
			Form::closeForm();
		?>
		<!--
		<div class='ifexist'>

		</div>-->

	</div>
</div>
<div class='intro_column'>

</div>
<?php
require_once('../includes/footer.php');
?>
